package com.app.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

public class NetworkUtil {

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm != null &&cm.getActiveNetworkInfo() !=null)
		{
			return cm.getActiveNetworkInfo().isAvailable();
		}
		// } else {
		// 　　　　　　　//如果仅仅是用来判断网络连接
		// 　　　　　　 //则可以使用 cm.getActiveNetworkInfo().isAvailable();
		// NetworkInfo[] info = cm.getAllNetworkInfo();
		// if (info != null) {
		// for (int i = 0; i < info.length; i++) {
		// if (info[i].getState() == NetworkInfo.State.CONNECTED) {
		// return true;
		// }
		// }
		// }
		// }
		return false;
	}

	/**
	 * 判断WIFI是否打开
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isWifiEnabled(Context context) {
		ConnectivityManager mgrConn = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkINfo = mgrConn.getActiveNetworkInfo();
		if (networkINfo != null
				&& networkINfo.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		}

		return false;
		// TelephonyManager mgrTel = (TelephonyManager) context
		// .getSystemService(Context.TELEPHONY_SERVICE);
		// return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
		// .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) ||
		// mgrTel
		// .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
	}

}
